home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2 Examples.sit / Raven 1.2 Examples / Quill / Source / ValidatorEditor.cpp < prev    next >
Text File  |  1997-08-10  |  14KB  |  502 lines

  1. /*
  2.  *  File:       ValidatorEditor.cpp
  3.  *  Summary:       A view that knows how to edit a TTextBox's validator.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996-1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <2>     4/06/97    JDJ        Updated for new style TSubPaneIterator.
  12.  *         <1>    11/30/96    JDJ        Created
  13.  */
  14.  
  15. #include "ValidatorEditor.h"
  16.  
  17. #include <ZDialogUtils.h>
  18. #include <ZFloatConversions.h>
  19. #include <ZRadioButton.h>
  20. #include <ZStaticText.h>
  21. #include <ZStringUtils.h>
  22. #include <ZTextValidators.h>
  23.  
  24.  
  25. // ===================================================================================
  26. //    class CValidatorCommand
  27. // ===================================================================================
  28. class CValidatorCommand : public TUndoableCommand {
  29.  
  30.     typedef TUndoableCommand Inherited;
  31.  
  32. //-----------------------------------
  33. //    Initialization/Destruction
  34. //
  35. public:
  36.     virtual             ~CValidatorCommand();
  37.     
  38.                         CValidatorCommand(TTextBox* pane, TTextValidatorPtr oldInfo, TTextValidatorPtr newInfo);
  39.     
  40. //-----------------------------------
  41. //    New API
  42. //
  43. public:
  44.     static    void         UpdatePane(TTextBox* pane, TTextValidatorPtr newInfo);
  45.  
  46. //-----------------------------------
  47. //    Inherited API
  48. //
  49. public:
  50.     virtual string        GetText() const;
  51.  
  52. protected:
  53.     virtual void         OnDo();
  54.  
  55.     virtual void         OnUndo();
  56.  
  57.     virtual void         OnRedo();
  58.             
  59. //-----------------------------------
  60. //    Member data
  61. //
  62. protected:
  63.     TTextBox*            mPane;
  64.     TTextValidatorPtr    mOldInfo;
  65.     TTextValidatorPtr    mNewInfo;
  66. };
  67.  
  68.  
  69. //---------------------------------------------------------------
  70. //
  71. // CValidatorCommand::~CValidatorCommand
  72. //
  73. //---------------------------------------------------------------
  74. CValidatorCommand::~CValidatorCommand()
  75. {
  76. }
  77.  
  78.  
  79. //---------------------------------------------------------------
  80. //
  81. // CValidatorCommand::CValidatorCommand
  82. //
  83. //---------------------------------------------------------------
  84. CValidatorCommand::CValidatorCommand(TTextBox* pane, TTextValidatorPtr oldInfo, TTextValidatorPtr newInfo)
  85. {
  86.     ASSERT(pane != nil);
  87.     
  88.     mPane = pane;
  89.     
  90.     mOldInfo = oldInfo;
  91.     mNewInfo = newInfo;
  92.  
  93.     TWindow* window = dynamic_cast<TWindow*>(mPane->GetTopView());
  94.     if (window != nil)
  95.         mContext = window->GetUndoContext();
  96.     else
  97.         mContext = nil;
  98.     mDelete  = mContext == nil;
  99. }
  100.  
  101.  
  102. //---------------------------------------------------------------
  103. //
  104. // CValidatorCommand::UpdatePane                        [static]
  105. //
  106. //---------------------------------------------------------------
  107. void CValidatorCommand::UpdatePane(TTextBox* pane, TTextValidatorPtr info)
  108. {
  109.     ASSERT(pane != nil);
  110.     
  111.     pane->SetValidator(info);
  112. }
  113.  
  114.  
  115. //---------------------------------------------------------------
  116. //
  117. // CValidatorCommand::GetText
  118. //
  119. //---------------------------------------------------------------
  120. string CValidatorCommand::GetText() const
  121. {
  122.     return "";                                // should be inside a transaction
  123. }
  124.  
  125.  
  126. //---------------------------------------------------------------
  127. //
  128. // CValidatorCommand::OnDo
  129. //
  130. //---------------------------------------------------------------
  131. void CValidatorCommand::OnDo()
  132. {
  133.     CValidatorCommand::UpdatePane(mPane, mNewInfo);
  134. }
  135.  
  136.  
  137. //---------------------------------------------------------------
  138. //
  139. // CValidatorCommand::OnUndo
  140. //
  141. //---------------------------------------------------------------
  142. void CValidatorCommand::OnUndo()
  143. {
  144.     CValidatorCommand::UpdatePane(mPane, mOldInfo);
  145. }
  146.  
  147.  
  148. //---------------------------------------------------------------
  149. //
  150. // CValidatorCommand::OnRedo
  151. //
  152. //---------------------------------------------------------------
  153. void CValidatorCommand::OnRedo()
  154. {
  155.     CValidatorCommand::UpdatePane(mPane, mNewInfo);
  156. }
  157.  
  158. #pragma mark -
  159.  
  160. // ===================================================================================
  161. //    CValidatorEditor
  162. // ===================================================================================
  163.  
  164. static TReanimatorRegister<CValidatorEditor> sValidatorEditorRegistrar;
  165.  
  166. //---------------------------------------------------------------
  167. //
  168. // CValidatorEditor::~CValidatorEditor
  169. //
  170. //---------------------------------------------------------------
  171. CValidatorEditor::~CValidatorEditor()
  172. {
  173. }
  174.  
  175.  
  176. //---------------------------------------------------------------
  177. //
  178. // CValidatorEditor::CValidatorEditor
  179. //
  180. //---------------------------------------------------------------
  181. CValidatorEditor::CValidatorEditor(TView* superView) : CRootPaneEditor(superView)
  182. {
  183.     mPane = nil;
  184. }
  185.  
  186.  
  187. //---------------------------------------------------------------
  188. //
  189. // CValidatorEditor::SetPane
  190. //
  191. //---------------------------------------------------------------
  192. void CValidatorEditor::SetPane(TPane* pane)
  193. {
  194.     ASSERT(pane != nil);
  195.     ASSERT(mPane == nil);
  196.     
  197.     mPane = dynamic_cast<TTextBox*>(pane);
  198.     ASSERT(mPane != nil);
  199.     
  200.     mOldInfo = mPane->GetValidator();
  201.         
  202.     // Listen to the radio buttons.
  203.     TSubPaneIterator iter = this->begin();
  204.     while (iter != this->end()) {
  205.         TPane* subPane = *iter;
  206.         ++iter;
  207.         
  208.         if (TRadioButton* button = dynamic_cast<TRadioButton*>(subPane))            
  209.             button->AddListener(this);
  210.     }
  211.     
  212.     // Initialize the text boxes with the correct default values.
  213.     TTextValidatorPtr info;
  214.     
  215.     info = TTextValidatorPtr(new TIntegerValidator);
  216.     this->SetEditorInfo(info);
  217.     
  218.     info = TTextValidatorPtr(new TFloatValidator);
  219.     this->SetEditorInfo(info);
  220.  
  221.     info = TTextValidatorPtr(new TAlphaValidator);
  222.     this->SetEditorInfo(info);
  223.  
  224.     info = TTextValidatorPtr(new TAlphaNumValidator);
  225.     this->SetEditorInfo(info);
  226.  
  227.     info = TTextValidatorPtr(new TPrintableValidator);
  228.     this->SetEditorInfo(info);
  229.  
  230.     // Initialize the editor with the current value.
  231.     this->SetEditorInfo(mOldInfo);
  232. }
  233.  
  234.  
  235. //---------------------------------------------------------------
  236. //
  237. // CValidatorEditor::Create                                [static]
  238. //
  239. //---------------------------------------------------------------
  240. MReanimatable* CValidatorEditor::Create(MReanimatable* parent)
  241. {
  242.     return new CValidatorEditor(dynamic_cast<TView*>(parent));
  243. }
  244.  
  245.  
  246. //---------------------------------------------------------------
  247. //
  248. // CValidatorEditor::Validate
  249. //
  250. //---------------------------------------------------------------
  251. bool CValidatorEditor::Validate()
  252. {
  253.     bool valid = Inherited::Validate();
  254.     
  255.     if (valid) {
  256.         bool done = false;
  257.         
  258.         TSubPaneIterator iter = this->begin();
  259.         while (iter != this->end() && !done) {
  260.             TPane* subPane = *iter;
  261.             ++iter;
  262.             
  263.             if (TRadioButton* button = dynamic_cast<TRadioButton*>(subPane)) {            
  264.                 if (button->GetValue() == 1) {
  265.                     string name = button->GetName();
  266.                     
  267.                     if (name == "Integer") {
  268.                         TTextBox* minView = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Min"));
  269.                         TTextBox* maxView = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Max"));
  270.                         
  271.                         if (minView->GetValue() > maxView->GetValue()) {
  272.                             DoStop(LoadAppString("Min integer value must be less than or equal to max value."), "");
  273.                             valid = false;
  274.                         }
  275.                         
  276.                     } else if (name == "Float") {
  277.                         TTextBox* minView = dynamic_cast<TTextBox*>(this->FindSubPane("Float Min"));
  278.                         TTextBox* maxView = dynamic_cast<TTextBox*>(this->FindSubPane("Float Max"));
  279.                         
  280.                         if (StrToDouble(minView->GetText()) > StrToDouble(maxView->GetText())) {
  281.                             DoStop(LoadAppString("Min float value must be less than or equal to max value."), "");
  282.                             valid = false;
  283.                         }    
  284.                     } 
  285.                         
  286.                     done = true;
  287.                 }
  288.             }
  289.         }
  290.     }
  291.     
  292.     return valid;
  293. }
  294.  
  295.  
  296. //---------------------------------------------------------------
  297. //
  298. // CValidatorEditor::Apply
  299. //
  300. //---------------------------------------------------------------
  301. void CValidatorEditor::Apply()
  302. {
  303.     TTextValidatorPtr info = this->GetEditorInfo();
  304.     
  305.     this->SetPaneInfo(info);
  306. }
  307.  
  308.  
  309. //---------------------------------------------------------------
  310. //
  311. // CValidatorEditor::Commit
  312. //
  313. //---------------------------------------------------------------
  314. void CValidatorEditor::Commit(TMultipleUndoableCommand* command)
  315. {
  316.     TTextValidatorPtr newInfo = this->GetEditorInfo();
  317.  
  318.     command->AddCommand(new CValidatorCommand(mPane, mOldInfo, newInfo));
  319. }
  320.  
  321.  
  322. //---------------------------------------------------------------
  323. //
  324. // CValidatorEditor::Revert
  325. //
  326. //---------------------------------------------------------------
  327. void CValidatorEditor::Revert()
  328. {
  329.     this->SetEditorInfo(mOldInfo);
  330.     this->SetPaneInfo(mOldInfo);
  331. }
  332.  
  333.  
  334. //---------------------------------------------------------------
  335. //
  336. // CValidatorEditor::GetEditorInfo        
  337. //
  338. //---------------------------------------------------------------
  339. TTextValidatorPtr CValidatorEditor::GetEditorInfo() const
  340. {
  341.     TTextValidatorPtr info;
  342.     
  343.     TSubPaneIterator iter = this->begin();
  344.     while (iter != this->end() && info.Get() == nil) {
  345.         TPane* subPane = *iter;
  346.         ++iter;
  347.         
  348.         if (TRadioButton* button = dynamic_cast<TRadioButton*>(subPane)) {            
  349.             if (button->GetValue() == 1) {
  350.                 string name = button->GetName();
  351.                 
  352.                 if (name == "Integer") {
  353.                     TTextBox* minView = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Min"));
  354.                     TTextBox* maxView = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Max"));
  355.                     info = TTextValidatorPtr(new TIntegerValidator(minView->GetValue(), maxView->GetValue()));
  356.                     
  357.                 } else if (name == "Float") {
  358.                     TTextBox* minView = dynamic_cast<TTextBox*>(this->FindSubPane("Float Min"));
  359.                     TTextBox* maxView = dynamic_cast<TTextBox*>(this->FindSubPane("Float Max"));
  360.                     info = TTextValidatorPtr(new TFloatValidator(StrToDouble(minView->GetText()), StrToDouble(maxView->GetText())));
  361.                     
  362.                 } else if (name == "Alpha") {
  363.                     TTextBox* numView = dynamic_cast<TTextBox*>(this->FindSubPane("Alpha Chars"));
  364.                     info = TTextValidatorPtr(new TAlphaValidator(numView->GetValue()));
  365.                     
  366.                 } else if (name == "AlphaNum") {
  367.                     TTextBox* numView = dynamic_cast<TTextBox*>(this->FindSubPane("AlphaNum Chars"));
  368.                     info = TTextValidatorPtr(new TAlphaNumValidator(numView->GetValue()));
  369.                     
  370.                 } else if (name == "Printable") {
  371.                     TTextBox* numView = dynamic_cast<TTextBox*>(this->FindSubPane("Printable Chars"));
  372.                     info = TTextValidatorPtr(new TPrintableValidator(numView->GetValue()));
  373.  
  374.                 } else
  375.                     DEBUGSTR("CValidatorEditor::GetEditorInfo found a bogus radio button: %s", name.c_str());
  376.             }
  377.         }
  378.     }
  379.     
  380.     return info;
  381. }
  382.  
  383.  
  384. //---------------------------------------------------------------
  385. //
  386. // CValidatorEditor::SetEditorInfo
  387. //
  388. //---------------------------------------------------------------
  389. void CValidatorEditor::SetEditorInfo(TTextValidatorPtr info)
  390. {
  391.     TRadioButton* button = nil;
  392.     TStaticText* caption1 = nil;
  393.     TStaticText* caption2 = nil;
  394.     TTextBox* textBox1 = nil;
  395.     TTextBox* textBox2 = nil;
  396.     
  397.     button = dynamic_cast<TRadioButton*>(this->FindSubPane("Integer"));
  398.     caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("Integer Caption"));
  399.     caption2 = dynamic_cast<TStaticText*>(this->FindSubPane("Integer Caption2"));
  400.     textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Min"));
  401.     textBox2 = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Max"));
  402.     if (TIntegerValidator* intVal = dynamic_cast<TIntegerValidator*>(info.Get())) {
  403.         button->SetValue(1);
  404.         caption1->Show();
  405.         caption2->Show();
  406.         textBox1->Show(); textBox1->SetValue(intVal->GetMin());
  407.         textBox2->Show(); textBox2->SetValue(intVal->GetMax());
  408.     } else {
  409.         button->SetValue(0);
  410.         caption1->Hide();
  411.         caption2->Hide();
  412.         textBox1->Hide(); 
  413.         textBox2->Hide(); 
  414.     }
  415.     
  416.     button = dynamic_cast<TRadioButton*>(this->FindSubPane("Float"));
  417.     caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("Float Caption"));
  418.     caption2 = dynamic_cast<TStaticText*>(this->FindSubPane("Float Caption2"));
  419.     textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("Float Min"));
  420.     textBox2 = dynamic_cast<TTextBox*>(this->FindSubPane("Float Max"));
  421.     if (TFloatValidator* floatVal = dynamic_cast<TFloatValidator*>(info.Get())) {
  422.         button->SetValue(1);
  423.         caption1->Show();
  424.         caption2->Show();
  425.         textBox1->Show(); textBox1->SetText(Strip(DoubleToStr(floatVal->GetMin()), " "));
  426.         textBox2->Show(); textBox2->SetText(Strip(DoubleToStr(floatVal->GetMax()), " "));
  427.     } else {
  428.         button->SetValue(0);
  429.         caption1->Hide();
  430.         caption2->Hide();
  431.         textBox1->Hide(); 
  432.         textBox2->Hide(); 
  433.     }
  434.     
  435.     button = dynamic_cast<TRadioButton*>(this->FindSubPane("Alpha"));
  436.     caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("Alpha Caption"));
  437.     textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("Alpha Chars"));
  438.     if (TAlphaValidator* alphaVal = dynamic_cast<TAlphaValidator*>(info.Get())) {
  439.         button->SetValue(1);
  440.         caption1->Show();
  441.         textBox1->Show(); textBox1->SetValue(alphaVal->GetMaxChars());
  442.     } else {
  443.         button->SetValue(0);
  444.         caption1->Hide();
  445.         textBox1->Hide(); 
  446.     }
  447.     
  448.     button = dynamic_cast<TRadioButton*>(this->FindSubPane("AlphaNum"));
  449.     caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("AlphaNum Caption"));
  450.     textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("AlphaNum Chars"));
  451.     if (TAlphaNumValidator* alphaNumVal = dynamic_cast<TAlphaNumValidator*>(info.Get())) {
  452.         button->SetValue(1);
  453.         caption1->Show();
  454.         textBox1->Show(); textBox1->SetValue(alphaNumVal->GetMaxChars());
  455.     } else {
  456.         button->SetValue(0);
  457.         caption1->Hide();
  458.         textBox1->Hide(); 
  459.     }
  460.     
  461.     button = dynamic_cast<TRadioButton*>(this->FindSubPane("Printable"));
  462.     caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("Printable Caption"));
  463.     textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("Printable Chars"));
  464.     if (TPrintableValidator* printVal = dynamic_cast<TPrintableValidator*>(info.Get())) {
  465.         button->SetValue(1);
  466.         caption1->Show();
  467.         textBox1->Show(); textBox1->SetValue(printVal->GetMaxChars());
  468.     } else {
  469.         button->SetValue(0);
  470.         caption1->Hide();
  471.         textBox1->Hide(); 
  472.     }
  473. }
  474.  
  475.  
  476. //---------------------------------------------------------------
  477. //
  478. // CValidatorEditor::SetPaneInfo
  479. //
  480. //---------------------------------------------------------------
  481. void CValidatorEditor::SetPaneInfo(TTextValidatorPtr info)
  482. {
  483.     CValidatorCommand::UpdatePane(mPane, info);
  484. }
  485.  
  486.  
  487. //---------------------------------------------------------------
  488. //
  489. // CValidatorEditor::OnBroadcast
  490. //
  491. //---------------------------------------------------------------
  492. void CValidatorEditor::OnBroadcast(const SControlMessage& mesg)
  493. {
  494.     #pragma unused(mesg)
  495.     
  496.     TTextValidatorPtr newInfo = this->GetEditorInfo();
  497.     this->SetEditorInfo(newInfo);
  498. }
  499.  
  500.  
  501.  
  502.